using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using Visualization;

namespace VisualizationPackage.FileLoader
{
    public class Loader
    {
        public static Mesh LoadFile(string fileName)
        {
            Mesh mesh = new Mesh(fileName);
            return mesh;
            DataFile dtFile = new DataFile();
            StreamReader sr = new StreamReader(fileName);
            
            Regex regTitle = new Regex("^title\\s*=\\s*\"(?<title>.*)\"$", RegexOptions.IgnoreCase);
            Regex regVariables = new Regex("^variables\\s*=\\s*((\\s*\"\\s*(?<var>[^\"\\s]*)\\s*\"\\s*(,\\s*|\\s*))*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Regex regFormat = new Regex("^zone\\s*t\\s*=\\s*\"(?<zoneName>[^\"]*)\"\\s*n\\s*=\\s*(?<n>\\d*)\\s*,\\s*e\\s*=\\s*(?<e>\\d*)\\s*,\\s*f\\s*=\\s*(?<format>\\w*)\\s*et\\s*=\\s*(?<elementType>\\w*)", RegexOptions.IgnoreCase);
            Regex regLine = new Regex("^\\s*((?<value>[\\d\\-\\.e\\+]+)\\s*)*", RegexOptions.IgnoreCase);
            Regex regTopology = new Regex("^\\s*((?<vert>\\d+)\\s*)*", RegexOptions.IgnoreCase);

            string line;
            Match m;
            while ((line = sr.ReadLine()) != null)
            {
                m = regTitle.Match(line);
                if (m.Success)
                {
                    dtFile.Title = m.Groups["title"].Value;
                    continue;
                }

                m = regVariables.Match(line);
                if (m.Success)
                {
                    foreach (Capture varName in m.Groups["var"].Captures)
                        dtFile._variables.Add(varName.Value);
                    continue;
                }

                m = regFormat.Match(line);
                if (m.Success)
                {
                    Zone z = new Zone();
                    z.Name = m.Groups["zoneName"].Value;
                    if (m.Groups["e"].Success)
                    {
                        z.Elements = int.Parse(m.Groups["e"].Value);
                        z.Topology = new List<int>(z.Elements * (int)z.ElementType);
                    }
                    if (m.Groups["n"].Success)
                    {
                        z.nVertics = int.Parse(m.Groups["n"].Value);
                        z.Vertics = new List<Vertex>(z.nVertics);
                    }
                    if (m.Groups["format"].Success)
                        z.Format = Util.ParseFormat(m.Groups["format"].Value);
                    if (m.Groups["elementType"].Success)
                        z.ElementType = Util.ParseElementType(m.Groups["elementType"].Value);
                    dtFile.Zones.Add(z);
                    continue;
                }

                m = regLine.Match(line);
                if (m.Success)
                    if (dtFile.Zones[dtFile.Zones.Count - 1].Vertics.Count < dtFile.Zones[dtFile.Zones.Count - 1].nVertics)
                    {
                        if (dtFile.Zones.Count == 0)
                            throw new Exception("Zones should be declared before vertics");
                        Zone z = dtFile.Zones[dtFile.Zones.Count - 1];
                        Vertex v = new Vertex();
                        int i = 0;
                        foreach (string varName in dtFile.Variables)
                            v[varName.ToUpper()] = double.Parse(m.Groups["value"].Captures[i++].Value);
                        z.Vertics.Add(v);
                        continue;
                    }
                    //m = regTopology.Match(line);
                    else
                    {
                        if (dtFile.Zones.Count == 0)
                            throw new Exception("Zones should be declared before vertics");
                        Zone z = dtFile.Zones[dtFile.Zones.Count - 1];
                        foreach (Capture vertNum in m.Groups["value"].Captures)
                            z.Topology.Add((int)double.Parse(vertNum.Value));
                        continue;
                    }
            }// while ((line = sr.ReadLine()) != null);

            sr.Close();
            //return dtFile;
        }

    }
}
    